package com.sc.auction.auction.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sc.auction.auction.Lock.AquiredLockWorker;
import com.sc.auction.auction.Lock.DistributedLocker;
import com.sc.auction.auction.feign.UserFeignService;
import com.sc.auction.auction.pojo.Auction;
import com.sc.auction.auction.pojo.Handsel;
import com.sc.auction.auction.pojo.Toppricee;
import com.sc.auction.auction.service.AuctionService;
import com.sc.auction.auction.service.HandselService;
import com.sc.auction.auction.Lock.ToppriceeService;
import com.sc.auction.common.pojo.User;
import com.sc.auction.common.util.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pgc
 * @since 2020-09-11
 */
@CrossOrigin
@RestController
@RequestMapping("/auction/toppricee")
public class ToppriceeController {
    @Autowired
    ToppriceeService toppriceeService;
    @Autowired
    UserFeignService userFeignService;
    @Autowired
    HandselService handselService;
    @Autowired
    AuctionService auctionService;
    @Autowired
    private DistributedLocker distributedLocker;

    @ApiOperation("增加竞价记录")
    @GetMapping(value = "/postTopprice")
    public R postTopprice(@RequestParam("token") String token,
                          @RequestParam("price")BigDecimal price,
                          @RequestParam("auctionid")Integer auctionid) throws Exception {
        System.out.println("jinrucifangfa"+"--"+price+"--"+auctionid);

        distributedLocker.lock("auctionid",new AquiredLockWorker<Object>() {

            @Override
            public Object invokeAfterLockAquire() {
                try {
                    System.out.println("进入分布式锁执行方法！");
//之后
                    R a=userFeignService.getLoginInfo(token);
                    R b=userFeignService.getUserById((Integer) a.getData().get("id"));
                    User user= JSON.toJavaObject((JSONObject)b.getData().get("user"),User.class);
//        System.out.println(user);
                    QueryWrapper queryWrapper1=new QueryWrapper();
                    queryWrapper1.eq("id",auctionid);
                    Auction auction2=auctionService.getOne(queryWrapper1);

                    BigDecimal balance=user.getBalance().subtract(auction2.getSecurityMoney());

                    if( balance.compareTo(BigDecimal.ZERO)==1||balance.compareTo(BigDecimal.ZERO)==0  ){


                        //增加交易记录
                        Toppricee toppricee=new Toppricee();
                        toppricee.setAuctionid(auctionid);
                        Date date=new Date();
                        toppricee.setAuctiontime(date);
                        toppricee.setMoney(price);
                        toppricee.setUser(user.getId());
                        toppriceeService.save(toppricee);
                        //增加保证金列表
                        QueryWrapper queryWrapper =new QueryWrapper();
                        queryWrapper.eq("auctionid",auctionid);
                        List<Handsel> handsels=handselService.list(queryWrapper);
                        Boolean flag=false;
                        for (Handsel h:handsels) {
                            if( h.getUserId()==user.getId()){
                                flag=true;
                            }
                        }
                        if (!flag){
                            Auction auction=auctionService.getById(auctionid);
                            Handsel handsel=new Handsel();
                            handsel.setAuctionId(auctionid);
                            handsel.setUserId(user.getId());
                            handsel.setMoney(auction.getSecurityMoney());
                            handselService.save(handsel);

                            //更新余额
                            user.setBalance(balance);
                            System.out.println(user);
                            userFeignService.update(user);
                        }

                    }

//之前
                }catch (Exception e){
                    e.printStackTrace();
                    return R.error();
                }
                return null;
            }
        });

        return R.ok();
    }

    public void MM(){

    }

}

